public class Test {
    public static void main(String[] args) {
        try{
            int[] array = {1,2,3};
            array [100] = 10;
            array [0] = 10;
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println(e);
        }finally {
            System.out.println("finally 中的代码⼀定会执⾏");
        }

        System.out.println("如果没有抛出异常，或者异常被处理了，try-catch后的代码也会执⾏");
    }
//        public static void main10(String[] args) {
//            try {
//                readFile("invalid.txt");
//            } catch (FileNotFoundException e) {
//                System.out.println("捕获到 FileNotFoundException: " + e.getMessage());
//            } catch (IOException e) {
//                System.out.println("捕获到 IOException: " + e.getMessage());
//            }
//        }
//    }
    public static int getElement(int[] array, int index) {
        if (null == array) {
            throw new NullPointerException(" 传递的数组为null");
        }
        if (index < 0 || index >= array.length) {
            throw new ArrayIndexOutOfBoundsException("传递的数组下标越界");
        }
        return array[index];
    }

    public static void main9(String[] args) {
        int[] array = {1,2,3};
        getElement(array, 3);
    }
    public static void main8(String[] args) {
        int array[]={1,2,3,4,5};
        try {
            array=null;
            System.out.println(array.length);
            System.out.println(array[5]);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("捕获了异常！");
    }
    }
    public static void main7(String[] args) {
        int array[]={1,2,3,4,5};
        try {
            array=null;
            System.out.println(array.length);
            System.out.println(array[5]);
        }catch (ArrayIndexOutOfBoundsException|NullPointerException e){
            e.printStackTrace();
            System.out.println("捕获了异常！");
        }
    }
    public static void main6(String[] args) {
        int array[]={1,2,3,4,5};
        try {
            array=null;
            System.out.println(array.length);//NullPointerException
            System.out.println(array[5]);//ArrayIndexOutOfBoundsException
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
            System.out.println("数组越界异常!");
        }catch (NullPointerException e){
            e.printStackTrace();
            System.out.println("空指针异常！");
        }
    }
    public static void main5(String[] args) {
        try{
            int array[]={1,2,3};
            System.out.println(array[3]);
        }catch (NullPointerException e){
            System.out.println("异常！");
        }
    }
    public static void main4(String[] args) {
        try{
            System.out.println(10/0);
            System.out.println("除数不能未0！");
        }catch (ArithmeticException e){
            e.printStackTrace();
            System.out.println("try中捕获了算术异常！");
        }
    }
    public static void main3(String[] args) {
        int[] array=null;
        System.out.println(array.length);
    }
    public static void main2(String[] args) {
        int[] array={1,2,3,4,5};
        System.out.println(array[100]);
    }
    public static void main1(String[] args) {
        System.out.println(100/0);
    }
}
